{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import bqplot.pyplot as plt\n",
    "from bqplot import CATEGORY10, ColorScale"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "size = 100\n",
    "np.random.seed(0)\n",
    "\n",
    "x_data = range(size)\n",
    "y_data = np.random.randn(size)\n",
    "y_data_2 = np.random.randn(size)\n",
    "y_data_3 = np.cumsum(np.random.randn(size) * 100.)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Basic Bar Chart"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(title='Bar Chart')\n",
    "plt.bar(np.arange(10), np.random.rand(10))\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Horizontal Bar Chart"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To generate a horizontal bar chart, pass `orientation='horizontal'` to the bar."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(title='Horizontal Bar Chart')\n",
    "plt.bar(np.arange(10), np.random.uniform(-1, 1, 10), orientation='horizontal')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Changing the reference value from which the Bars are drawn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = plt.figure()\n",
    "# assign the output of the plt.bar to a mark object\n",
    "bar = plt.bar(x_data[:20], np.abs(y_data_2[:20]), base=1.0)\n",
    "# render the figure\n",
    "fig"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# changing the base\n",
    "bar.base = 2.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "bar.align = 'right'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Bar Chart Properties"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Increasing the spacing between the bars using padding property\n",
    "fig = plt.figure()\n",
    "bar = plt.bar(x_data[:20], y_data[:20], padding=0.3)\n",
    "fig"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# changing basic properties like stroke and opacity\n",
    "bar.stroke = 'red'\n",
    "bar.opacities = [0.5, 0.2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "bar.orientation = 'horizontal'\n",
    "fig.axes[0].orientation = 'vertical'\n",
    "fig.axes[1].orientation = 'horizontal'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Stacked Bar Chart for 2-d data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = plt.figure()\n",
    "bar = plt.bar(x_data, [y_data[:20], y_data_2[:20]], \n",
    "              padding=0.2,\n",
    "              colors=CATEGORY10)\n",
    "fig"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Grouped Bar Chart"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "bar.type = 'grouped' # equivalent to saying\n",
    "# plt.bar(x_data, y_data, padding=0.2, type='grouped')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = plt.figure()\n",
    "bar = plt.bar(x_data, [y_data[:20], y_data_2[:20]], \n",
    "              padding=0.2, \n",
    "              colors=CATEGORY10, \n",
    "              orientation='horizontal')\n",
    "fig"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "bar.type = 'grouped'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Modifying color mode"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## Color mode has 2 values. 'group' and 'element'. \n",
    "## 'group' means for every x all bars have same color.\n",
    "## 'element' means for every dimension of y, all bars have same color.\n",
    "fig = plt.figure()\n",
    "bar = plt.bar(x_data, [y_data[:20], y_data_2[:20]], padding=0.2, colors=CATEGORY10, color_mode='group')\n",
    "fig"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## for 1-d array for Y.\n",
    "fig = plt.figure()\n",
    "bar = plt.bar(x_data, y_data[:20], padding=0.2, color_mode='element', \n",
    "              labels=['Values'], display_legend=True)\n",
    "fig"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## Representing additional dimension using Color"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# In this example, the color is just the magnitude of the y data\n",
    "fig = plt.figure()\n",
    "# add a 'reds' color scale \n",
    "plt.scales(scales={'color': ColorScale(scheme='Reds')})\n",
    "\n",
    "bar = plt.bar(x_data[:20], y_data[:20], color=np.abs(y_data[:20]), padding=0.2)\n",
    "\n",
    "# give enough bottom margin to accommodate the color axis\n",
    "fig.fig_margin = dict(top=50, bottom=80, left=50, right=50)\n",
    "\n",
    "fig"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Adding color for 2-d data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# By default color is applied along the axis=1\n",
    "fig = plt.figure()\n",
    "plt.scales(scales={'color': ColorScale(scheme='Reds')})\n",
    "\n",
    "y_vals = [y_data[:20], y_data_2[:20], y_data_3[:20] / 100.0]\n",
    "color_data = np.mean(y_vals, axis=1)\n",
    "\n",
    "bar = plt.bar(x_data, y_vals, color=color_data, padding=0.2,\n",
    "           labels=['Dim 1', 'Dim 2', 'Dim 3'], display_legend=True)\n",
    "\n",
    "fig.fig_margin = dict(top=50, bottom=80, left=50, right=50)\n",
    "fig"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Applying color along the axis=0\n",
    "fig = plt.figure()\n",
    "plt.scales(scales={'color': ColorScale(mid=0.)})\n",
    "\n",
    "y_vals = [y_data[:20], y_data_2[:20], y_data_3[:20] / 100.0]\n",
    "color_data = np.mean(y_vals, axis=0)\n",
    "\n",
    "bar = plt.bar(x_data, y_vals, color=color_data, padding=0.2, \n",
    "              color_mode='group', stroke='orange')\n",
    "fig.fig_margin = dict(top=50, bottom=80, left=50, right=50)\n",
    "fig"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
